# SYSTEM SETTINGS SECTION

DICTIONARY_DIR = 'dictionary'
ADDITIONS_DIR = 'additions'
ENCODING = 'utf-8'
DELIMITER = '\t'

# END OF SYSTEM SETTINGS

import os
import time
import codecs

import wordnet


def load_MainDict():
	global mainDict, mainDictWC
	mainDict = {}
	for fileName in os.listdir(DICTIONARY_DIR):
		if fileName.endswith('.txt'):
			filePath = os.path.join(DICTIONARY_DIR, fileName)
			lineNum = 1
			print 'ACTION: reading', filePath
			for line in codecs.open(filePath, encoding=ENCODING).readlines():
				columns = line.strip().split(DELIMITER)
				if len(columns) >= 3:
					word = columns[0].strip()
					category = columns[1].strip()
					description = columns[2].strip()
					if mainDict.has_key(word):
						mainDict[word] += '(%s) %s\n' % (category, description)
					else:
						mainDict[word] = '(%s) %s\n' % (category, description)
				else:
					print 'WARNING: invalid entry at line', lineNum
				lineNum += 1
	mainDictWC = len(mainDict.keys())


def load_AddedDict():
	global addedDict, addedDictWC, addedFilePath
	addedDict = {}
	for fileName in	os.listdir(ADDITIONS_DIR):
		if fileName.endswith('.txt'):
			filePath = os.path.join(ADDITIONS_DIR, fileName)
			lineNum = 1
			print 'ACTION: reading', filePath
			for line in codecs.open(filePath, encoding=ENCODING).readlines():
				line = line.strip()
				columns = line.split(DELIMITER)
				if len(columns) >= 3:
					word = columns[1].strip()
					category = columns[2].strip()
					description = columns[3].strip()
					if addedDict.has_key(word):
						addedDict[word] += '(%s) %s\n' % (category, description)
					else:
						addedDict[word] = '(%s) %s\n' % (category, description)
				else:
					print 'WARNING: invalid entry at line', lineNum
					lineNum += 1
	addedFilePath = os.path.join(ADDITIONS_DIR, 'added_%s.txt' % (time.strftime('%Y%m%d_%H%M%S')))
	addedDictWC = len(addedDict.keys())


def lookup_WordNet(lookup):
	try:	# If the lookup word is not in ASCII range, it will raise exception
		lookup = str(lookup)
		message = ''
		if wordnet.V.has_key(lookup):	# If the word is a Verb
			message = "(v) " + str(wordnet.V[lookup].getSenses()[0].synset.gloss)

		if wordnet.N.has_key(lookup):	# If the word is a Noun
			message += "\n" + "(n) " + str(wordnet.N[lookup].getSenses()[0].synset.gloss)
	
		if wordnet.ADJ.has_key(lookup):	# If the word is an Adjective
			message += "\n" + "(adj) " + str(wordnet.ADJ[lookup].getSenses()[0].synset.gloss)
	
		if wordnet.ADV.has_key(lookup):	# If the word is an Adverb
			message += "\n" + "(adv) " + str(wordnet.ADV[lookup].getSenses()[0].synset.gloss)
	except StandardError:
		return None
	return message
